package com.lushixiang.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Order(2)
@Slf4j
@WebFilter(filterName = "UserStatusFilter",description = "给所有的请求设置session",urlPatterns = "/session" )
public class SessionFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig){
        log.info("用户cookie设置过滤器");
    }

    @Override
    public void destroy() {
        log.info("用户过滤器销毁");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

        Cookie[] cookies = httpServletRequest.getCookies();
        if(cookies == null || cookies.length == 0){
            log.info("cookie 长度为0，将设置永久cookie和60秒cookie");
            Cookie foreverCookie = new Cookie("foreverCookie","I-am-forever-cookie");
            Cookie tempCookie = new Cookie("tempCookie","I-am-temp-Cookie");

            foreverCookie.setMaxAge(-1);
            foreverCookie.setHttpOnly(false);
            tempCookie.setMaxAge(60);
            tempCookie.setHttpOnly(false);
            httpServletResponse.addCookie(foreverCookie);
            httpServletResponse.addCookie(tempCookie);

        }
        else{
            log.info("cookie 并不为null 或者空");
            boolean havaTempCookie = false;
            boolean havaForeverCookie = false;
            for (Cookie cookie : cookies) {
                if(!havaForeverCookie && "foreverCookie".equals(cookie.getName())){
                    havaForeverCookie = true;
                }
                if(!havaTempCookie && "tempCookie".equals(cookie.getName())){
                    havaTempCookie = true;
                }
            }
            if(!havaForeverCookie){
                log.info("不存在永久cookie，增加永久cookie");
                Cookie foreverCookie = new Cookie("foreverCookie","I-am-forever-cookie");
                foreverCookie.setMaxAge(-1);
                foreverCookie.setHttpOnly(false);
                httpServletResponse.addCookie(foreverCookie);
            }else{
                log.info("存在foreverCookie");
            }
            if(!havaTempCookie){
                log.info("不存在暂时cookie，增加暂时cookie");
                Cookie tempCookie = new Cookie("tempCookie","I-am-temp-Cookie");
                tempCookie.setMaxAge(60);
                tempCookie.setHttpOnly(false);
                httpServletResponse.addCookie(tempCookie);
            }else{
                log.info("存在tempCookie");
            }

        }
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
